package com.example.controller;

import com.example.entity.Student;
import com.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/students")
    public String getAllStudents(
            @RequestParam(required = false) String keyword,
            Model model,
            HttpSession session) {

        // 检查登录状态
        if (session.getAttribute("loggedIn") == null) {
            return "redirect:/index";
        }

        List<Student> students;
        if (keyword != null && !keyword.isEmpty()) {
            students = studentService.searchStudents(keyword);
        } else {
            students = studentService.getAllStudents();
        }

        model.addAttribute("students", students);
        model.addAttribute("userType", session.getAttribute("userType"));
        return "student";
    }

    // 其他方法保持不变...
}